package com.dragon.modules.yuegou.service.impl.act;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.act.OffMemberActivityEntity;
import com.dragon.modules.yuegou.enums.act.OffMemberActivityIsPrize;
import com.dragon.modules.yuegou.enums.code.ActivityCode;
import com.dragon.modules.yuegou.excel.act.ActivityDataWorkbook;
import com.dragon.modules.yuegou.mapper.act.OffMemberActivityMapper;
import com.dragon.modules.yuegou.model.act.LotteryJoinInfo;
import com.dragon.modules.yuegou.model.act.OffMemberActivity;
import com.dragon.modules.yuegou.model.system.Document;
import com.dragon.modules.yuegou.query.act.OffMemberActivityQuery;
import com.dragon.modules.yuegou.service.act.OffMemberActivityService;
import com.dragon.modules.yuegou.service.system.DocumentService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员参与活动表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-11-19 10:57:52
 * @version 2.0
 */
@Service("actOffMemberActivityService")
public class OffMemberActivityServiceImpl
		extends ServiceImpl<OffMemberActivityMapper, OffMemberActivityEntity>
		implements OffMemberActivityService {

    @Autowired
    private DocumentService documentService;

    @Override
    public Document exportData(String id, String mobile, OffMemberActivityIsPrize isPrize) {
        Check.notEmpty(id, ActivityCode.ACTIVITY_ID_NOT_EMPTY.getMessage());

        OffMemberActivityQuery query = new OffMemberActivityQuery();
        query.setActivityId(id);
        query.setMemberMobile(mobile);
        query.setIsPrize(isPrize);
        QueryWrapper<OffMemberActivityEntity> wrapper =  query.toQueryWrapper();
        wrapper.orderByDesc(OffMemberActivityEntity.COL_CREATED_TIME);
        List<OffMemberActivityEntity> list = list(wrapper);

        ActivityDataWorkbook activityData = new ActivityDataWorkbook();

        List<ActivityDataWorkbook.MemberActivityRow> memberActivityRows = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OffMemberActivityEntity memberActivity : list) {
                ActivityDataWorkbook.MemberActivityRow memberActivityRow = new ActivityDataWorkbook.MemberActivityRow();
                memberActivityRow.setMobile(memberActivity.getMemberMobile());
                memberActivityRow.setGrade(memberActivity.getGrade());
                memberActivityRow.setIsPrize(memberActivity.getIsPrize().getAlias());
                memberActivityRows.add(memberActivityRow);
            }
        }
        activityData.setMemberActivityRows(memberActivityRows);
        return documentService.generateExcel("活动数据记录表", activityData);
    }

    @Override
    public List<OffMemberActivity> memberInfo(String id) {
        Check.notEmpty(id, "活动Id不能为空");
        IPage<OffMemberActivityEntity> pageEntity = new Page<>(1, 20, true);
        //设置分页排序

        OffMemberActivityQuery memberActivityQuery = new OffMemberActivityQuery();
        memberActivityQuery.setActivityId(id);
        QueryWrapper<OffMemberActivityEntity> wrapper = memberActivityQuery.toQueryWrapper();
        wrapper.orderByDesc(OffMemberActivity.COL_CREATED_TIME);

        //获取最新参与的20条记录
        IPage<OffMemberActivityEntity> entityIPage = baseMapper.selectPage(pageEntity, wrapper);
        IPage<OffMemberActivity> iPage = entityIPage.convert(OffMemberActivity::new);
        iPage.getRecords().forEach(memberActivity -> {
            String mobile = memberActivity.getMemberMobile();
            if (StringUtils.isNotEmpty(mobile)) {
                memberActivity.setMemberMobile(mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            }
            //大汉年会抽奖-用于返回总数,又不影响原来的结构 by 余胜国
            memberActivity.setTotal(iPage.getTotal());
        });

        return iPage.getRecords();
    }

    @Override
    public List<LotteryJoinInfo> getJoinList(String activityId) {
        Check.notEmpty(activityId, "活动Id不能为空");
        IPage<OffMemberActivityEntity> pageEntity = new Page<>(1, 50, true);
        //只返回50个
        OffMemberActivityQuery memberActivityQuery = new OffMemberActivityQuery();
        memberActivityQuery.setActivityId(activityId);
        IPage<OffMemberActivityEntity> iPage = baseMapper.selectPage(pageEntity, memberActivityQuery.toQueryWrapper());
        return iPage.getRecords().stream()
                .filter(memberActivity->StringUtils.isNotEmpty(memberActivity.getMemberHead()))
                .map(memberActivity -> {
                    LotteryJoinInfo info = new LotteryJoinInfo();
                    info.setId(memberActivity.getMemberId());
                    info.setImage(memberActivity.getMemberHead());
                    return info;
                }).collect(Collectors.toList());
    }

    @Override
    public boolean isJoin(String activityId, String memberId) {
        OffMemberActivityQuery memberActivityQuery = new OffMemberActivityQuery();
        memberActivityQuery.setActivityId(activityId);
        memberActivityQuery.setMemberId(memberId);
        Long count = count(memberActivityQuery.toQueryWrapper());
        return count > 0;
    }
}